package com.znjc.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.znjc.common.constant.DeviceConstants;
import com.znjc.device.domain.DevicePoint;
import com.znjc.device.domain.DeviceRuleRef;
import com.znjc.device.domain.IotDataResRule;
import com.znjc.device.mapper.DeviceRuleRefMapper;
import com.znjc.device.service.DeviceDataService;
import com.znjc.device.service.IDevicePointService;
import com.znjc.device.service.IDeviceRuleRefService;
import com.znjc.device.service.IIotDataResRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author zengpc
 * @date 2023-12-04
 */
@Service
public class DeviceRuleRefServiceImpl extends ServiceImpl<DeviceRuleRefMapper, DeviceRuleRef> implements IDeviceRuleRefService {
    @Autowired
    IDevicePointService devicePointService;
    @Autowired
    IIotDataResRuleService iotDataResRuleService;
    @Autowired
    private DeviceDataService deviceDataService;

    @Override
    public Boolean verify(DeviceRuleRef ref) {
        LambdaQueryWrapper<DeviceRuleRef> wrap = new LambdaQueryWrapper<>();
        wrap.eq(DeviceRuleRef::getDeviceId, ref.getDeviceId());
        wrap.eq(DeviceRuleRef::getResRuleId, ref.getResRuleId());
        wrap.eq(DeviceRuleRef::getPointId, ref.getPointId());
        if (ref.getId() != null) {
            wrap.ne(DeviceRuleRef::getId, ref.getId());
        }
        int count = this.count(wrap);
        if (count > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Long getPointId(Long deviceId, Long ruleId) {
        LambdaQueryWrapper<DeviceRuleRef> wrap = new LambdaQueryWrapper<>();
        wrap.eq(DeviceRuleRef::getDeviceId, deviceId);
        wrap.eq(DeviceRuleRef::getResRuleId, ruleId);
        DeviceRuleRef ruleRef = this.getOne(wrap);
        return null == ruleRef ? null : ruleRef.getPointId();
    }

    @Override
    public List<DeviceRuleRef> getListByDeviceId(Long deviceId) {
        LambdaQueryWrapper<DeviceRuleRef> wrap = new LambdaQueryWrapper<>();
        wrap.eq(DeviceRuleRef::getDeviceId, deviceId);
        wrap.orderByAsc(DeviceRuleRef::getSort);
        return this.list(wrap);
    }

    @Override
    public void deleteRefByDeviceId(Long deviceId) {
        LambdaQueryWrapper<DeviceRuleRef> wrap = new LambdaQueryWrapper<>();
        wrap.eq(DeviceRuleRef::getDeviceId, deviceId);
        this.remove(wrap);
    }

    @Override
    public void deleteRefByPointId(Long pointId) {
        LambdaQueryWrapper<DeviceRuleRef> wrap = new LambdaQueryWrapper<>();
        wrap.eq(DeviceRuleRef::getPointId, pointId);
        this.remove(wrap);
    }

    @Override
    public void deleteRefByRuleId(Long ruleId) {
        LambdaQueryWrapper<DeviceRuleRef> wrap = new LambdaQueryWrapper<>();
        wrap.eq(DeviceRuleRef::getResRuleId, ruleId);
        this.remove(wrap);
    }

    @Override
    public void autoBindingPointAndRule(Long typeId, Long deviceId) {
        List<DevicePoint> points = devicePointService.getListByTypeId(typeId);
        if (CollectionUtils.isNotEmpty(points)) {
            List<IotDataResRule> rules = iotDataResRuleService.getListByTypeId(typeId);
            if (CollectionUtils.isNotEmpty(rules)) {
                List<DeviceRuleRef> list = new ArrayList<>();
                AtomicInteger i = new AtomicInteger();
                points.stream().forEach(point -> {
                    rules.stream().forEach(rule -> {
                        if (point.getPointName().equals(rule.getRuleName())) {
                            DeviceRuleRef entity = new DeviceRuleRef();
                            entity.setDeviceId(deviceId);
                            entity.setPointId(point.getId());
                            entity.setResRuleId(rule.getId());
                            entity.setSort(i.get());
                            list.add(entity);
                            i.getAndIncrement();
                        }
                    });
                });
                if (CollectionUtils.isNotEmpty(list)) {
                    boolean flag = this.saveBatch(list);
                    /*if (flag) {
                        list.stream().forEach(ref -> {
                            DevicePoint point = devicePointService.getById(ref.getPointId());
                            if (null != point) {
                                deviceDataService.addColumn(DeviceConstants.TABLE_NAME + ref.getDeviceId(), point.getPointCode());
                                deviceDataService.addColumn(DeviceConstants.TABLE_NAME + ref.getDeviceId(), point.getPointCode() + DeviceConstants.COLUMN_NAME);
                            }
                        });
                    }*/
                }
            }
        }

    }
}
